package com.ybzl.mini.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.QueryChainWrapper;
import com.ybzl.common.core.controller.BaseController;
import com.ybzl.common.core.controller.IdGenerator;
import com.ybzl.common.core.domain.ResultVo;
import com.ybzl.common.core.page.PageData;
import com.ybzl.common.utils.SecurityUtils;
import com.ybzl.mini.entity.MiniGood;
import com.ybzl.mini.entity.MiniGoodPics;
import com.ybzl.mini.mapper.MiniGoodPicsMapper;
import com.ybzl.mini.service.IMiniGoodService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/*
 * Desc：商品表
 * @Author jiaxin
 * @Company asiainfo.com
 * @Since  2024-06-30 15:57:17.258
 *
 */

@Slf4j
@RestController
@RequestMapping("/miniGood")
public class MiniGoodController extends BaseController {

    @Autowired
    private IMiniGoodService iMiniGoodService;

    @Autowired
    private MiniGoodPicsMapper miniGoodPicsMapper;

    /**
     * 新增 MiniWenzhang
     *
     * @param miniGood miniGood
     * @return data data
     */
    @PostMapping("/add")
    public ResultVo add(@RequestBody MiniGood miniGood) {
        if (null == miniGood.getId()) {
            Long id = IdGenerator.orderId(MiniGood.class);
            miniGood.setId(id);
            miniGood.setStatus("0");
            iMiniGoodService.save(miniGood);
            List<MiniGoodPics> pics = miniGood.getPics();
            for(MiniGoodPics tmp : pics){
                tmp.setGoodId(id);
                tmp.setId(IdGenerator.orderId(MiniGoodPics.class));
                miniGoodPicsMapper.insert(tmp);
            }
        } else {
            iMiniGoodService.updateById(miniGood);
            miniGoodPicsMapper.delete(new LambdaQueryWrapper<MiniGoodPics>().eq(MiniGoodPics::getGoodId,miniGood.getId()));
            List<MiniGoodPics> pics = miniGood.getPics();
            for(MiniGoodPics tmp : pics){
                tmp.setGoodId(miniGood.getId());
                tmp.setId(IdGenerator.orderId(MiniGoodPics.class));
                miniGoodPicsMapper.insert(tmp);
            }
        }
        return ResultVo.success();

    }


    /**
     * 更新 MiniWenzhang
     *
     * @param miniGood miniGood
     * @retur data data
     */
    @PutMapping("/update")
    public ResultVo update(@RequestBody MiniGood miniGood) {
        miniGood.setUpdateBy(SecurityUtils.getUsername());
        iMiniGoodService.updateById(miniGood);
        return ResultVo.success();
    }

    /**
     * 查询 MiniGood 列表，模糊查询
     *
     * @param miniGood miniGood
     * @return data data
     */
    @PostMapping("/list")
    public PageData list(@RequestBody MiniGood miniGood) {
        startPage();
        LambdaQueryWrapper<MiniGood> query = new LambdaQueryWrapper<>();
        query.orderByAsc(MiniGood::getSort);
        List<MiniGood> miniGoodList = iMiniGoodService.list(query);
        for (MiniGood temp : miniGoodList) {
            temp.setTypeName(iMiniGoodService.queryInfo("mini_good_type",temp.getType()));
            temp.setTypeTwoName(iMiniGoodService.queryInfo("mini_good_stype",temp.getTypeTwo()));
        }
        return getDataTable(miniGoodList);
    }

    /**
     * 根据主键查询
     *
     * @param keyId keyId
     * @return info info
     */
    @GetMapping("/detail/{keyId}")
    public ResultVo detail(@PathVariable("keyId") String keyId) {
        MiniGood miniGood = iMiniGoodService.getById(keyId);
        List<MiniGoodPics> pics = miniGoodPicsMapper.selectList(new LambdaQueryWrapper<MiniGoodPics>().eq(MiniGoodPics::getGoodId,miniGood.getId()));
        miniGood.setPics(pics);
        return ResultVo.success(miniGood);
    }

    @DeleteMapping("/delete/{keyId}")
    public ResultVo delete(@PathVariable("keyId") String keyIds) {

        if (keyIds.indexOf(",") > 0) {
            String[] keyIdArray = keyIds.split(",");
            for (String key : keyIdArray) {
                iMiniGoodService.removeById(key);
            }
        }
        iMiniGoodService.removeById(keyIds);
        return ResultVo.success();
    }
}
